package com.xposed.aliproxy.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.xposed.aliproxy.db.DBUtil;
import com.xposed.aliproxy.xposed.utils.XMerTradeDetailRequestCallBack;

import org.apache.http.cookie.SM;
import de.robv.android.xposed.XposedBridge;

/**
 * 交易码广播接收
 */
public class AliTradeNoReceiver extends BroadcastReceiver {
    private Context context;
    private DBUtil dbUtil;

    public AliTradeNoReceiver(Context context, DBUtil dBUtil) {
        this.context = context;
        this.dbUtil = dBUtil;
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals("com.abcd.xpouseali.action.alitradeno.received")) {
            String stringExtra = intent.getStringExtra("tradeNo");
            String stringExtra2 = intent.getStringExtra("cookie");
            String stringExtra3 = intent.getStringExtra("name");
            if (!this.dbUtil.queryAlitradeByTradeNo(stringExtra)) {
                StringBuilder stringBuilder = new StringBuilder();
                stringBuilder.append("https://tradeeportlet.alipay.com/wireless/tradeDetail.htm?tradeNo=");
                stringBuilder.append(stringExtra);
                stringBuilder.append("&source=channel&_from_url=https%3A%2F%2Frender.alipay.com%2Fp%2Fz%2Fmerchant-mgnt%2Fsimple-order._h_t_m_l_%3Fsource%3Dmdb_card");
                String stringBuilder2 = stringBuilder.toString();
                try {
                    HttpUtils httpUtils = new HttpUtils(15000);
                    httpUtils.configResponseTextCharset("GBK");
                    RequestParams requestParams = new RequestParams();
                    requestParams.addHeader(SM.COOKIE, stringExtra2);
                    httpUtils.send(HttpMethod.GET, stringBuilder2, requestParams, new XMerTradeDetailRequestCallBack(context, stringExtra, stringExtra3));
                } catch (Throwable th) {
                    XposedBridge.log(th);
                }
            }
        }
    }
}
